package com.guigu.controller;

import com.guigu.pojo.Product;
import com.guigu.service.Pservice;
import com.guigu.vo.Pojo;
import com.guigu.vo.Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("/product")
@CrossOrigin
@Controller
public class Pcontroller {
    @Autowired
    private Pservice pservice;
    @RequestMapping("/select")
    @ResponseBody
    public Pojo<Product> selectProduct(@RequestBody Vo vo, Integer pageNo, Integer pageSize) {
        System.out.println(vo);
        System.out.println(pageNo);
        System.out.println(pageSize);
        Pojo<Product> pojo = pservice.selectProduct(vo, pageNo, pageSize);
        System.out.println(pojo);
        return pojo;
    }
    //删除
   @DeleteMapping("/{id}")
    @ResponseBody
    public String delete(@PathVariable int id) {
        System.out.println(id);
       pservice.removeById(id);
        System.out.println("删除");
        return "ok";
    }
    //图片上传
@PostMapping("/image")
    @ResponseBody
    public String image( MultipartFile icon) throws IOException {
        System.out.println(icon);
        //获取文件名
String orgFileName = icon.getOriginalFilename();
System.out.println(orgFileName);
        //获取文件后缀
String suffix = orgFileName.substring(orgFileName.lastIndexOf("."));
        //生成新的文件名
String newFileName = System.currentTimeMillis() + suffix;
System.out.println(newFileName);
        //上传文件
File newfile =new File("D:\\study\\files\\fuwuqi\\"+newFileName);
icon.transferTo(newfile);
        return "http://localhost:10001/images/"+newFileName;
    }

}
